import asyncio
import logging
from websockets import client
from datetime import datetime
from aiowebsocket.converses import AioWebSocket


async def startup(uri, header):
    async with AioWebSocket(uri, headers=header) as aws:
        print(11111111111111111)
        converse = aws.manipulator
        print(22222222222222222222)
        message = '{"action":"join","uuid":"3c1ae9f6-4805-463a-9563-6bc0fc7ec113","type":"room","room_id":138394231}'
        print(12312312)

        while True:
            await converse.send(message)
            print('{time}-Client send: {message}'
                  .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
            mes = await converse.receive()
            print('{time}-Client receive: {rec}'
                  .format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))

#CLIENT_PROXY_ADDRESS = "wss://proxy.fm.missevan.com:9002"
if __name__ == '__main__':
    remote = 'wss://fm.missevan.com:3016/ws'
    header = [
        'GET wss://fm.missevan.com:3016/ws HTTP/1.1',
        'Host: fm.missevan.com:3016',
        'Connection: Upgrade',
        'Pragma: no-cache',
        'Cache-Control: no-cache',
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36',
        'Upgrade: websocket',
        'Origin: https://fm.missevan.com',
        'Sec-WebSocket-Version: 13',
        'Accept-Encoding: gzip, deflate, br',
        'Accept-Language: zh-CN,zh;q=0.9',
        'Sec-WebSocket-Key: JLlYHw4PPJXkKyA01ovG7Q==',
        'Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits'
    ]
    try:
        print('准备执行')
        asyncio.get_event_loop().run_until_complete(startup(remote, header))
    except KeyboardInterrupt as exc:
        logging.info('Quit.')

'''GET wss://fm.missevan.com:3016/ws HTTP/1.1
Host: fm.missevan.com:3016
Connection: Upgrade
Pragma: no-cache
Cache-Control: no-cache
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36
Upgrade: websocket
Origin: https://fm.missevan.com
Sec-WebSocket-Version: 13
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9
Sec-WebSocket-Key: JLlYHw4PPJXkKyA01ovG7Q==
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits

'''